package com.yuanfengerp.purchase.service.impl;


import com.yuanfengerp.publicUtil.Result;
import com.yuanfengerp.purchase.dao.IPurchaseReceiptDao;
import com.yuanfengerp.purchase.entity.PurchaseReceipt;
import com.yuanfengerp.purchase.service.IPurchaseReceiptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseReceiptServiceImpl implements IPurchaseReceiptService {
    @Autowired
    private IPurchaseReceiptDao ipad;

    @Override
    public Result<List<PurchaseReceipt>> queryPurchaseReceipt(Map<String, Object> params) {
        List<PurchaseReceipt> purchaseReceipts = ipad.queryPurchaseReceipt(params);
        if (!purchaseReceipts.isEmpty()) {
            return Result.success(purchaseReceipts);
        } else {
            return Result.failure(400, "暂无数据");
        }
    }

    @Override
    public Result<String> addPurchaseReceipt(PurchaseReceipt purchaseReceipt) {
        purchaseReceipt.setPurchase_receipt_actual_arrival_date(new Date());
        purchaseReceipt.setPurchase_receipt_operation_time(new Date());
        purchaseReceipt.setPurchase_receipt_audit_time(new Date());
        int i = ipad.addPurchaseReceipt(purchaseReceipt);
        if (i > 0) {
            return Result.success("添加成功");
        } else {
            return Result.failure(400, "添加失败");
        }
    }

    @Override
    public Result<PurchaseReceipt> queryPurchaseReceiptById(String purchase_receipt_id) {
        PurchaseReceipt purchaseReceipt = ipad.queryPurchaseReceiptById(purchase_receipt_id);
        if (purchaseReceipt != null) {
            return Result.success(purchaseReceipt);
        } else {
            return Result.failure(400, "暂无数据");
        }
    }

    @Override
    public Result<String> updatePurchaseReceiptById(PurchaseReceipt purchaseReceipt) {
        int i = ipad.updatePurchaseReceiptById(purchaseReceipt);
        if (i > 0) {
            return Result.success("修改成功");
        } else {
            return Result.failure(400, "修改失败");
        }
    }

    @Override
    public Result<String> deletePurchaseReceipt(String purchase_receipt_id) {
        int i = ipad.deletePurchaseReceipt(purchase_receipt_id);
        if (i > 0) {
            return Result.success("删除成功");
        } else {
            return Result.failure(400, "删除失败");
        }
    }

    @Override
    public Result<String> updatePurchaseReceiptAgree(String purchase_receipt_id) {
        //判断对应ID的订单状态是否为1与2，如果是,则返回当前订单状态为1——审核通过，2——审核拒绝，如果不是则将订单状态改为1
        int i = ipad.updatePurchaseReceiptAgree(purchase_receipt_id);
        if (i > 0) {
            return Result.success("审核通过" + purchase_receipt_id);
        } else {
            return Result.failure(400, "审核状态为" + 1 + "，请勿重复审核");
        }
    }

    @Override
    public Result<String> updatePurchaseReceiptRefuse(String purchase_receipt_id) {
        int i = ipad.updatePurchaseReceiptRefuse(purchase_receipt_id);
        if (i > 0) {
            return Result.success("审核拒绝" + purchase_receipt_id);
        } else {
            return Result.failure(400, "审核状态为" + 2 + "，请勿重复审核");
        }
    }
}


